package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result selectDept(){
        log.info("获取部门全部数据");

        List<Dept> deptList=deptService.list();

        System.out.println("傻逼李城");
        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除部门：{}",id);

        deptService.delete(id);

        return Result.success();
    }

    /**
     * 新增部门//         注意封装josn数据是request不是response
     * @param dept
     * @return
     */
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门",dept.getName());

        System.out.println(dept.getName());
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */

    @GetMapping ("/{id}")
    public Result selectId(@PathVariable Integer id){
        log.info("根据id查询",id);

        Dept deptList=deptService.selectId(id);
        return Result.success(deptList);
    }

    @PutMapping
    public Result upDept(@RequestBody Dept dept){
        System.out.println(dept);
        log.info("更新部门");
        deptService.upDept(dept);

        return Result.success();
    }
}
